console.log(uni.getStorageSync('token'), 'uni.getStorageInfoSync(token)');
import {uploadUrl} from './config.js';

//export const uploadUrl = 'http://192.168.0.111:8080'
//export const uploadUrl = 'http://localhost:8080'
const $request = (url, data, method = 'POST', headers = "application/json;") => {
    return new Promise((resolve, reject) => {
        // uni.showLoading({
        // 	title: '数据加载中',
        // 	icon: 'loading',
        // 	mask: true
        // });
        let header = {
            'Content-Type': headers,
        }
        if (uni.getStorageSync('token')) {
            header['Authorization'] =
                'tf: ' + uni.getStorageSync('token')
        }
        uni.request({
            // 服务器地址
            url: uploadUrl + url,
            //url: 'https://zuche.mylocation.cn/prod-api/' + url,
            // 测试地址
            //url: 'http://192.168.0.118:8080/' + url,
            method: method,
            data: data,
            header: header,
            success(res) {
                if (res.data.code == 401) {
                    // 获取当前页面栈
                    const pages = getCurrentPages();
                    // 获取当前页面的实例
                    const currentPage = pages[pages.length - 1];
                    const options = currentPage.options
                    uni.showModal({
                        title: '提示',
                        content: '当前需要登录授权才能进行，是否进行登录授权',
                        success: (res => {
                            if (res.confirm) {
                                // 获取页面路由
                                const route = `/pages/login/login?geturl=/${currentPage.route}&options=${JSON.stringify(options)}`;
                                console.log('请求\'tf:', route, options)
                                uni.redirectTo({url: route})
                            } else {
                                reject(res.data.data);
                            }

                        }),
                        error: (res) => {
                            reject(res.data.data);
                            uni.showToast({
                                title: '接口正在维护',
                                icon: "error"
                            })
                        }
                    })
                    // reject(res.data.data);
                } else {
                    resolve(res)
                }

                // uni.hideLoading(); //关闭loading
            },
            fail(error) {
                console.log(error);
                uni.showToast({
                    title: '网络访问请求错误',
                    icon: "none"
                });
                reject(error);
                // throw new Error(error);
                // uni.hideLoading(); //关闭loading
            },
            complete() {
                // uni.hideLoading(); //关闭loading
            }
        })
    })
}

const $uploadFile = (url, data) => {
    return new Promise((resolve, reject) => {
        uni.uploadFile({
            url: uploadUrl + url,
            filePath: data,
            name: 'file',
            formData: {
                // 'user': 'test'
            },
            success: (res) => {
                resolve(JSON.parse(res.data))
            },
            fail: (error) => reject(error)
        });
    })
}

const $uploadFiles = (url, data) => {
    return new Promise((resolve, reject) => {
        uni.uploadFile({
            url: uploadUrl + url,
            filePath: data,
            name: 'files',
            formData: {
                // 'user': 'test'
            },
            success: (res) => {
                resolve(JSON.parse(res.data))
            },
            fail: (error) => reject(error)
        });
    })
}
//get请求
const $get = (url, data) => {
    return $request(url, data, 'GET')
}
//post请求
const $post = (url, data) => {
    return $request(url, data, 'POST')
}
//post请求
const $put = (url, data) => {
    return $request(url, data, 'PUT')
}
//post请求
const $delete = (url, data) => {
    return $request(url, data, 'DELETE')
}
//给uniapp原生的requset赋值
//记得要在mian.js中引用
uni.$request = $request
uni.$get = $get
uni.$post = $post
uni.$put = $put
uni.$delete = $delete
uni.$uploadFile = $uploadFile
uni.$uploadFiles = $uploadFiles